package irri.soils.ssnm;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.Intent;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class ActivityMain6 extends SSNM implements View.OnClickListener, OnItemClickListener{
	private Button button1;
	private Button button2;
	private Button button3;
	private Bundle b;
	private ListView listView1;
	private ArrayAdapter<Object> adapter;
	private WebView webView1;
	private RelativeLayout relativeLayout1;
	private Intent intent;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main6);
        setCustomTitle();
        b = this.getIntent().getExtras();
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(this);
        button1.setEnabled(false);
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(this);
        button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(this);
        button3.setEnabled(false);
        
        listView1 = (ListView) findViewById(R.id.listView1);
        listView1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView1.setOnItemClickListener(this);
        webView1 = (WebView) findViewById(R.id.webView1);
        relativeLayout1 = (RelativeLayout) findViewById(R.id.relativeLayout1);
        
        displayView();
        selectAll();
        selectAll();
    }
    
    public void displayView(){
    	adapter = new ArrayAdapter<Object>(this, R.layout.list_black_text, 
	    		fileList());
        listView1.setAdapter(adapter);
    }
    
    private void selectAll(){
    	boolean b = button2.getText().equals("Select all");
    	int count = listView1.getCount();
    	for(int i=0; i<count; i++)
            listView1.setItemChecked(i, !b);
        for(int i=0; i<count; i++)
        listView1.performItemClick(listView1, i, R.id.list_content1);
    }
    

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.button1:
			try{
				SparseBooleanArray theArray = listView1.getCheckedItemPositions();
				for(int i = 0; i < theArray.size(); i++)
					if(theArray.get(i)){
						InputStream is = openFileInput(fileList()[i]);
						if (is!=null) {
							InputStreamReader inputreader = new InputStreamReader(is);
						    BufferedReader buffreader = new BufferedReader(inputreader);
						    String line, response = "";
						      // read every line of the file into the line-variable, on line at the time
						      while (( line = buffreader.readLine()) != null) {
						    	  	response += line;
						      }
						      
					    	  webView1.getSettings().setJavaScriptEnabled(true);
					    	  webView1.setWebViewClient(new WebViewClient());
					    	  webView1.loadDataWithBaseURL("file:///android_asset/", 
					    			  response.replace("<>", " "), "text/html", "utf-8", null);
					    	  webView1.setBackgroundColor(0);
					    	  relativeLayout1.setVisibility(View.GONE);
						}
					}
			}catch(Exception ex){
				
			}
			break;
		case R.id.button2:
			selectAll();
			break;
		case R.id.button3:
			try{
				SparseBooleanArray theArray = listView1.getCheckedItemPositions();
				String[] filenames = fileList();
				for(int i = 0; i < theArray.size(); i++)
					if(theArray.get(i)){
						this.deleteFile(filenames[i]);
					}
				intent = new Intent(ActivityMain6.this, ActivityMain6.class);
	      	    intent.putExtras(b);
	      	    startActivity(intent);
	            finish();
			}catch(Exception ex){
				
			}
			break;
		}
	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent(ActivityMain6.this, SSNM.prevActivity==null ? 
				ActivityMain3.class : SSNM.prevActivity.getClass());
		intent.putExtras(b);
		startActivity(intent);
		finish();
	}

	/* (non-Javadoc)
	 * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		SparseBooleanArray theArray = listView1.getCheckedItemPositions();
		int counter = 0; for(int i = 0; i < theArray.size(); i++){ 
			if(theArray.get(i)){ 
				counter++; }}
		button1.setEnabled(counter==1);
		button2.setText(counter==listView1.getCount() ? "Unselect all" : "Select all");
		button3.setEnabled(counter>0);
	}
}